package cn.wuxing.mediation.web.controller.business;

import cn.wuxing.mediation.businessplatform.domain.dto.AllCaseDataPageRequestDTO;
import cn.wuxing.mediation.businessplatform.service.IAllCaseDataService;
import cn.wuxing.mediation.common.core.domain.entity.AllCaseData;
import cn.wuxing.mediation.common.core.page.PageDomain;
import cn.wuxing.mediation.common.core.page.TableDataInfo;
import cn.wuxing.mediation.common.core.page.TableSupport;
import cn.wuxing.mediation.common.utils.EncryptionUtils;
import cn.wuxing.mediation.common.utils.poi.ExcelUtil;
import cn.wuxing.mediation.system.domain.MdjfReq;
import cn.wuxing.mediation.system.domain.vo.MdjfListVo;
import cn.wuxing.mediation.system.service.IMdkfListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static cn.wuxing.mediation.common.utils.PageUtils.startPage;

@Slf4j
@RestController
//@RequestMapping("/all_case")
@RequestMapping("/cm/mdjf")
public class AllCaseDataController {
    @Autowired
    private IMdkfListService dkfListService;

    private final IAllCaseDataService allCaseDataService;

    public AllCaseDataController(IAllCaseDataService allCaseDataService) {
        this.allCaseDataService = allCaseDataService;
    }

//    @GetMapping("/pagination")
    @GetMapping("list")
    public TableDataInfo<AllCaseData> allCasePagination(AllCaseDataPageRequestDTO requestDTO) {
//        startPage();
        log.info("【矛盾全量库】- 分页查询 - 请求参数：{}", requestDTO);
        return allCaseDataService.allCaseDataPagination(requestDTO);
    }

    @PostMapping("/export")
    public void export(HttpServletResponse response, MdjfReq req)
    {
        if(req.getIds()==null||req.getIds().size()==0){//如果没勾选,就导出当前分页
            PageDomain pageDomain = TableSupport.buildPageRequest();
            Integer pageNum = pageDomain.getPageNum();
            Integer pageSize = pageDomain.getPageSize();
            req.setCurrentPage((pageNum-1)*pageSize);
            req.setCurrentPageSize(pageSize);
        }
        List<MdjfListVo> list = dkfListService.queryListAll(req);
        ExcelUtil<MdjfListVo> util = new ExcelUtil<MdjfListVo>(MdjfListVo.class);
        list.forEach(i->{
            String[] dsr = i.getDsr().split(",");
            List<String> des = new ArrayList<>();
            try {
                des = Arrays.stream(dsr).map(EncryptionUtils::decrypt).collect(Collectors.toList());
            } catch (Exception ex) {
                log.error("解密失败", ex);
            } finally {
                if(!des.isEmpty()){
                    String collect = des.stream().map(str -> EncryptionUtils.mask(str, "**")).collect(Collectors.joining(","));
                    i.setDsr(collect);
                }else{
                    i.setDsr(EncryptionUtils.mask(i.getDsr(), "**"));
                }
            }
        });
        util.exportExcel(response, list, "矛盾纠纷全量库信息数据");
    }
}
